import { defineApis } from '@/utils/api'

// 分页查询用户列表
const queryUsers = {
    url: '/user'
}

// 添加用户
const addUser = {
    url: '/user',
    method: 'POST',
}

// 修改用户
const updateUser = {
    url: '/user',
    method: 'PUT',
}

// 修改用户状态
function updateUserStatus(userId: number, status: boolean) {
    return {
        url: `/user/status`,
        method: 'PUT',
        params: {
            userId,
            status
        }
    }
}

// 删除用户
function removeUser(id: number) {
    return {
        url: `/user/${id}`,
        method: 'DELETE',
    }
}

//查询某个用户的角色列表
function listUserRole(id: number) {
    return {
        url: '/user/role',
        params: {
            userId: id
        },
    }
}

//获取某个用户的角色及权限信息
const listUserSecurity = (id: number) => {
    return {
        url: '/user/security',
        params: {
            userId: id
        },
    }
}


//为某个用户增加角色
function associateUserRole(userId: number, roleId: number) {
    return {
        url: '/user/role',
        method: 'POST',
        params: {
            userId,
            roleId
        },
    }
}

//移除某个用户的某个角色
function disassociateUserRole(userId: number, roleId: number) {
    return {
        url: '/user/role',
        method: 'DELETE',
        params: {
            userId,
            roleId
        },
    }
}

//为某个用户增加权限
const associateUserPermission = {
    url: '/user/permission',
    method: 'POST',
}

//移除某个用户的某个权限
function disassociateUserPermission(userId: number, permissionCode: string) {
    return {
        url: '/user/permission',
        method: 'DELETE',
        params: {
            userId,
            permissionCode
        },
    }
}

//设置某个用户的角色及权限
const setUserSecurity = {
    url: '/user/security',
    method: 'PUT'
}

export default defineApis('admin', {
    queryUsers,
    addUser,
    updateUser,
    updateUserStatus,
    removeUser,
    listUserRole,
    listUserSecurity,
    associateUserRole,
    disassociateUserRole,
    associateUserPermission,
    disassociateUserPermission,
    setUserSecurity
})